﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GraphosData;
using System.Xml.Linq;
using GraphosControls;
using System.Xml.Serialization;

namespace GraphViewerTestApp
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
		}

		protected override void OnInitialized(EventArgs e)
		{
			base.OnInitialized(e);

			m_graph = new Graph();
			//m_graph.AddNodeTemp("Saul");
			//m_graph.AddNodeTemp("Abinadab");
			//m_graph.AddNodeTemp("Eshbaal");
			//m_graph.AddNodeTemp("Ish-bosheth");
			//m_graph.AddNodeTemp("Malchi-shua");
			//m_graph.AddNodeTemp("Ahinoam");
			//m_graph.AddNodeTemp("Rizpah");
			//m_graph.AddNodeTemp("Jonathan");
			//m_graph.AddNodeTemp("Ishvi");
			//m_graph.AddNodeTemp("Merab");
			//m_graph.AddNodeTemp("Michal");
			//m_graph.AddNodeTemp("David");
			//m_graph.AddNodeTemp("Armoni");
			//m_graph.AddNodeTemp("Mephibosheth");
			//m_graph.AddNodeTemp("Mephibosheth2");
			//m_graph.AddNodeTemp("Ziba");
			//m_graph.AddNodeTemp("Kish");
			//m_graph.AddNodeTemp("Abner");
			//m_graph.AddNodeTemp("Ner");

			//foreach (var node in m_graph.Nodes)
			//{
			//    if (node.Person.FirstName != "Saul")
			//    {
			//        m_graph.AddEdgeTemp("Saul", node.Person.FirstName);
			//        m_graph.AddEdgeTemp(node.Person.FirstName, "Saul");
			//    }
			//}

			//m_graph.AddEdgeTemp("Rizpah", "Armoni");
			//m_graph.AddEdgeTemp("Armoni", "Rizpah");
			//m_graph.AddEdgeTemp("Rizpah", "Mephibosheth");
			//m_graph.AddEdgeTemp("Mephibosheth", "Rizpah");
			//m_graph.AddEdgeTemp("Jonathan", "Mephibosheth2");
			//m_graph.AddEdgeTemp("Mephibosheth2", "Jonathan");
			//m_graph.AddEdgeTemp("Mephibosheth2", "Ziba");
			//m_graph.AddEdgeTemp("Ziba", "Mephibosheth2");
			//m_graph.AddEdgeTemp("Michal", "David");
			//m_graph.AddEdgeTemp("David", "Michal");
			//m_graph.AddEdgeTemp("Ner", "Abner");
			//m_graph.AddEdgeTemp("Abner", "Ner");
			//m_graph.AddEdgeTemp("Ner", "Kish");
			//m_graph.AddEdgeTemp("Kish", "Ner");

			const string fname = "firstname";
			const string lname = "lastname";
			const string id = "id";
			const string rel = "relationships";

			Person personOther;
			Person person;
			XDocument doc;

			doc = XDocument.Load("../../test_4.xml");


			Dictionary<string, Person> dictPeople = new Dictionary<string, Person>();

			XElement root = doc.Element("people");

			foreach (XElement p in root.Elements())
			{
				person = new Person();
				person.FirstName = (string)p.Element(fname);
				person.ID = (string)p.Element(id);
				person.LastName = (string)p.Element(lname);
				dictPeople[person.ID] = person;
				m_graph.AddNode(person);
			}

			foreach (XElement p in root.Elements())
			{
				person = dictPeople[(string)p.Element(id)];
				foreach (XElement XRel in p.Element(rel).Elements())
				{
					personOther = dictPeople[(string)XRel.Element(id)];
					//assign relationship type
					//switch ((string)XRel.Element(rel))
					//{
					//    case "lovers":
					//        relationEnum = Relation.lover;
					//        break;
					//    default:
					//        relationEnum = Relation.friend;
					//        break;
					//}
					//m_graph.AddEdge(person.ID, personOther.ID);
					m_graph.AddEdge(person.ID, personOther.ID);
					//nodePerson.AddEdge(nodeOther, relationEnum);
					//nodeOther.AddEdge(nodePerson, relationEnum);
				}
			}

			//foreach (KeyValuePair<string, Node> p in dictPeople)
			//{
			//    foreach (Edge ed in p.Value.Edges)
			//    {
			//        m_graph.AddEdge(p.Value, ed.Node, ed.Relation);
			//    }
			//}

			MainGraphViewer.Graph = m_graph;
		}

		private Graph m_graph;

		private void NamesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count <= 0)
				return;

			Node node = e.AddedItems[0] as Node;
			if (node == null)
				return;

			MainGraphViewer.SelectedNode = node;

		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			MainGraphViewer.RandomNodes();
		}


	}
}
